package cn.rongdao.jrkabao.ui.cashcoupon.mycashcoupon;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;
import com.mifengkong.kotlincommon.indicator.IndicatorAdapter;
import com.mifengkong.kotlincommon.indicator.TrackIndicatorView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.ui.cashcoupon.mycashcoupon.fragment.FragmentInvalid;
import cn.rongdao.jrkabao.ui.cashcoupon.mycashcoupon.fragment.FragmentUnUse;
import cn.rongdao.jrkabao.ui.cashcoupon.mycashcoupon.fragment.FragmentUsed;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jack on 2017/8/12
 *
 * @author jack
 */

public class MyCashCouponActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, MyCashCouponActivity.class);
    }

    @BindView(R.id.amcc_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.amcc_track_indicator_view)
    TrackIndicatorView mTrackIndicatorView;
    @BindView(R.id.amcc_view_pager)
    ViewPager mViewPager;

    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();

    @Override
    protected void initView() {
        initTitleBar();
        initFragmentAndDrawable();
        initTab();
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_my_cash_coupon;
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("我的现金券");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBarUIComponent.initRightTextWithColor("使用说明", R.color.white, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(thisActivity(), InfoConstant.CASH_COUPON_USE_EXPLIAN);
            }
        });
    }

    private void initFragmentAndDrawable() {
        Fragment fragmentUnUse = new FragmentUnUse();
        Fragment fragmentUsed = new FragmentUsed();
        Fragment fragmentInvalid = new FragmentInvalid();

        mFragmentList.add(fragmentUnUse);
        mTitleList.add("未使用");
        mFragmentList.add(fragmentUsed);
        mTitleList.add("已使用");
        mFragmentList.add(fragmentInvalid);
        mTitleList.add("已失效");
    }

    private void initTab() {
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        });
        mTrackIndicatorView.setAdapter(new IndicatorAdapter() {
            @Override
            public int getItemCount() {
                return mFragmentList.size();
            }

            @NotNull
            @Override
            public View getView(int position, @NotNull ViewGroup parent) {
                TextView tvTab = (TextView) LayoutInflater.from(thisActivity()).inflate(R.layout.layout_loan_tab, parent, false);
                tvTab.setText(mTitleList.get(position));
                return tvTab;
            }

            @Override
            public void highLightIndicator(@NotNull View view) {
                view.setSelected(true);
            }

            @Override
            public void resetIndicator(@NotNull View view) {
                view.setSelected(false);
            }

            @Override
            public View getBottomTrackView() {
                View view = new View(thisActivity());
                view.setLayoutParams(new ViewGroup.LayoutParams(FRDisplayMetrics.dp2px(thisActivity(),80),FRDisplayMetrics.dp2px(thisActivity(),2)));
                view.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
                return view;
            }
        }, mViewPager);
    }
}